/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-03-13 16:36:06
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-03-18 18:38:24
 * @FilePath: \dm\云课堂\js\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
$(function () {
    // 判断是否登录
    if (localStorage.getItem('userInfo')) {
        $('#userIn > .tips, .glyphicon').remove();
        var userInfo = JSON.parse(localStorage.getItem('userInfo'));
        $('#userIn').append(`
                    <div class="userIn">
                        <div class="userImg">
                            <img src="./images/userAvatar_1431_微信图片_20231103095832-1754844961339080704.jpg" alt="">
                        </div>
                        <div class="userName">
                            <span>${userInfo.nickname}</span>
                            <div class="userVip">
                            <img data-v-5c89a100="" src="" alt="">
                            青铜会员
                        </div>
                        </div>
                    </div>
                    `);
                    $('.goin').append(`
                    <div class="userIn">
                        <div class="userImg">
                            <img src="./images/userAvatar_1431_微信图片_20231103095832-1754844961339080704.jpg" alt="">
                        </div>
                        <div class="userName">
                            ${userInfo.nickname}
                        </div>
                        <div class="userVip">
                            <img data-v-5c89a100="" src="" alt="">
                            青铜会员
                        </div>
                    </div>
                    `);
            // 登录退出下拉菜单
        $('#userIn').hover(
            function() {
                $('.inDown').stop().slideToggle();
            },
            function() {
                $('.inDown').stop().slideToggle();
            }
        );
        $('.goin').hover(
            function() {
                $('.topDown').stop().slideToggle();
            },
            function() {
                $('.topDown').stop().slideToggle();
            }
        );
    }
    // 请求轮播图数据
    $.ajax({
        url: `https://wkt.myhope365.com//weChat/applet/course/banner/list?number=5`,
        type: 'get',
        success(res) {
            // console.log(res);
            if (res.code == 0) {
                res.data.forEach(item => {
                    $('.swiper-wrapper').append(`<div class="swiper-slide" style="background-image: url(${item.imgUrlPc});"></div>`
                    )
                })
            }
            var mySwiper = new Swiper ('.swiper', {
                loop: true, // 循环模式选项
                // 如果需要分页器
                pagination: {
                  el: '.swiper-pagination',
                },
                speed: 600,
                autoplay: true,
                // 如果需要前进后退按钮
                navigation: {
                  nextEl: '.swiper-button-next',
                  prevEl: '.swiper-button-prev',
                },
            })
        }
    })
    // 请求下拉列表数据
    $.ajax({
        url: `https://wkt.myhope365.com//weChat/applet/subject/list`,
        type: 'post',
        headers: {
            'Content-type': 'application/json',
        }, //请求头
        data: JSON.stringify({
            "enable": 1
        }),
        success(res) {
            // console.log(res);
            if (res.code == 0) {
                res.rows.forEach(item => 
                    $('.pull-down').append(`<li><a href="#">${item.title}</a></li>`)
                )
            }
        },
        error: function (err) {
            console.log(err) //返回的数据
        }, //失败后
    })
    // 请求课程数据
    function gatdata(types, className, box) {
        $.ajax({
            url: `https://wkt.myhope365.com/weChat/applet/course/list/type`,
            type: 'POST',
            data: {
                type: types,
                pageNum: 1,
                pageSize: 5
            },
            success(res) {
                // console.log(res);
                if (res.code == 0) {
                    res.rows.forEach(item => {
                        if (types == 'discount') {
                            $(className).append(`
                            <div class="${box}_text">
                                <div class="${box}_div">
                                    <a href="D:/vscode/dm/云课堂/details.html?${item.courseId} & ${types}" style="display: block;">
                                        <div class="${box}_img">
                                            <img src="${item.coverFileUrl}" alt="">
                                        </div>
                                    <a>
                                    <div class="text">
                                        <p><b>${item.courseTitle}</b></p>
                                        <p class="${box}_pp">共 ${item.subSectionNum} 节课 | ${item.participationsCount} 人报名</p>
                                        <span class="${box}_p"><b>${item.isFree == 1 ? '免费' : ''}</b><b style="color: red;">${item.isFree == 0 && item.isDiscount == 1 ? '￥' + item.discountPrice : ''}</b></span>
                                        <span><del>${item.isFree == 0 && item.isDiscount == 1 ? '￥' + item.coursePrice : ''}</del><span>
                                        <div data-v-f7710c5a="" class="discount-desc ant-tag ant-tag-orange">限时钜惠</div>
                                    </div>
                                </div>
                            </div>
                            `)
                        } else {
                            $(className).append(`
                        <div class="${box}_text">
                            <div class="${box}_div">
                                <a href="D:/vscode/dm/云课堂/details.html?${item.courseId} & ${types}" style="display: block;">
                                    <div class="${box}_img">
                                        <img src="${item.coverFileUrl}" alt="">
                                    </div>
                                <a>
                                <div class="text">
                                    <p><b>${item.courseTitle}</b></p>
                                    <p class="${box}_pp">共 ${item.subSectionNum} 节课 | ${item.participationsCount} 人报名</p>
                                    <span class="${box}_p"><b>${item.isFree == 1 ? '免费' : ''}</b><b style="color: red;">${item.isFree == 0 && item.isDiscount == 1 ? '￥' + item.discountPrice : ''}</b></span>
                                    <span><del>${item.isFree == 0 && item.isDiscount == 1 ? '￥' + item.coursePrice : ''}</del><span>
                                </div>
                            </div>
                        </div>
                        `)
                        }
                    })
                }
            },
            error: function (err) {
                console.log(err) //返回的数据
            }, //失败后
        })
    }
    gatdata('free', '.free_content', 'free');
    gatdata('boutique', '.premium_content', 'premium');
    gatdata('discount', '.discount_content', 'discount');
    // 下拉列表
    $('.none').hover(
        function() {
            $('.pull-down').stop().slideToggle();
        },
        function() {
            $('.pull-down').stop().slideToggle();
        }
    )
    // 登录与注册
    $('.user').click(function() {
        $(this).addClass('color_text').siblings('.pow').removeClass('color_text');
        $('#form-group').remove();
        $('.module_body').append(`
            <form class="form-group" id="form-group" action="">
                <div class="form-group input_user">
                    <img src="./images/用户名.png" alt="">
                    <input class="form-control" type="text" placeholder="请输入账户名">
                </div>
                <div class="form-group input_pow">
                    <img src="./images/密码.png" alt="">
                    <input class="form-control" type="password" placeholder="请输入密码">
                </div>
                <div class="text-right">
                    <button class="form-control btnIn" type="submit" style="color: #fff; background-color: #00CF8C; margin-top: 25px; margin-bottom: 20px;">登录</button>
                </div>
                <a href="" class="register href_a">立刻注册！</a>
                <a href="" class="forgot href_a">忘记密码？</a>
            </form>
        `);
    })
    $('.pow').click(function() {
        $(this).addClass('color_text').siblings('.user').removeClass('color_text');
        $('#form-group').remove();
        $('.module_body').append(`
            <form class="form-group" id="form-group" action="">
                <div class="form-group input_user">
                    <img src="./images/用户名.png" alt="">
                    <input class="form-control" type="text" placeholder="请输入手机号">
                </div>
                <div class="form-group input_pow verification">
                    <img src="./images/密码.png" alt="">
                    <input class="form-control" type="password" placeholder="请输入验证码" style="width: 158px;">
                    <button>获取验证码</button>
                </div>
                <div class="text-right">
                    <button class="form-control btnIn_1" type="submit" style="color: #fff; background-color: #00CF8C; margin-top: 0px; margin-bottom: 20px;">登录</button>
                </div>
                <a href="" class="register href_a">立刻注册！</a>
                <a href="" class="forgot href_a">忘记密码？</a>       
            </form> 
        `)
    })
    // 登录与注册请求
    $('.btnIn').click(function () {
        var user = $('#user').val();
        var pow =  $('#pow').val();
        $.ajax({
            url: `https://wkt.myhope365.com/pcUser/login`,
            type: 'POST',
            data: {
                username: user,
                password: pow,
                rememberMe: true,
            },
            success(res) {
                console.log(res);
                if (res.code == 500) {
                    alert('用户不存在/密码错误');
                }
                if (res.code == 0) {
                    alert('成功登录');
                    $('#register').modal('hide');
                    $('#userIn > .tips, .glyphicon').remove();
                    $('#userIn').append(`
                    <div class="userIn">
                        <div class="userImg">
                            <img src="./images/userAvatar_1431_微信图片_20231103095832-1754844961339080704.jpg" alt="">
                        </div>
                        <div class="userName">
                            ${res.userInfo.nickname}
                        </div>
                        <div class="userVip">
                            <img data-v-5c89a100="" src="" alt="">
                            青铜会员
                        </div>
                    </div>
                    `);
                    $('.goin').append(`
                    <div class="userIn">
                        <div class="userImg">
                            <img src="./images/userAvatar_1431_微信图片_20231103095832-1754844961339080704.jpg" alt="">
                        </div>
                        <div class="userName">
                            ${res.userInfo.nickname}
                        </div>
                        <div class="userVip">
                            <img data-v-5c89a100="" src="" alt="">
                            青铜会员
                        </div>
                    </div>
                    `);
                    window.location.reload();
                }
                // 本地存储
                localStorage.setItem('userInfo', JSON.stringify(res.userInfo));
            },
            error: function (err) {
                console.log(err) //返回的数据
            }, //失败后
        })
    })
    $('.out').click(function () {
        localStorage.removeItem('userInfo');
        window.location.reload();
    })
    // 搜索请求
    var tap = true;
    $('.btnGo').click(function () {
        if ($('.searchInput').val() == '') {
            $.ajax({
                url: `https://wkt.myhope365.com//pc/course/search/keyword?pageNum=5&pageSize=10`,
                type: 'GET',
                success(res) {
                    console.log(res);
                    if (res.code == 0) {
                        window.location.href = `file:///D:/vscode/dm/%E4%BA%91%E8%AF%BE%E5%A0%82/search.html?${tap}`
                    }
                },
                error: function (err) {
                    console.log(err) //返回的数据
                }, //失败后
            })
        } else {
            tap = false;
        }
    })
})
// 吸顶
window.onscroll = function() {
    if (document.documentElement.scrollTop >= 700) {
        $('.box_ceiling').css('visibility', 'visible')
    } else {
        $('.box_ceiling').css('visibility', 'hidden')
    }
}